package com.wlz.learnjuc.lock;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *  模拟并发，多个线程在某一时刻同时开始执行  -- 计数器    -- 等待其他线程执行完，再去执行主线程下面的逻辑
 *
 */
public class CountDownLatchTest {
    public static void main(String[] args) {
        // 闭锁
        CountDownLatch countDownLatch = new CountDownLatch(10);

        for (int i = 0; i < 11; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        synchronized (countDownLatch) {
                            //减1
                            countDownLatch.countDown();
                            System.out.println(Thread.currentThread().getName()+" counts = " +
                                    countDownLatch.getCount());
                        }
                        // 阻塞
                        countDownLatch.await();

                        System.out.println("concurrency counts = " + (10 - countDownLatch.getCount()));

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

        System.out.println("end ......");
    }
}

